/**
 * <pre>
 * 紫光软件系统有限公司广电事业部
 * Copyright (C): 2011
 * 
 * 文件名称：
 * com.unis.iptv.service.impl.VodServiceImpl.java
 * 
 * 文件描述: 
 * TODO
 * 
 * Notes:
 * 修改历史(作者/日期/改动描述):
 * 杨飞宇/2013-1-5/初始化版本。
 * </pre>
 */
package com.unis.iptv.service.impl;

import static com.unis.iptv.client.ClientCustomContext.downloadPictureByUrl;
import static com.unis.iptv.client.ClientCustomContext.getCustomContext;
import static com.unis.iptv.common.utils.GlobalUtil.baseUrl;

import java.net.URLEncoder;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.unis.iptv.common.utils.Authentication;
import com.unis.iptv.common.utils.GlobalUtil;
import com.unis.iptv.dao.CastRoleMapDao;
import com.unis.iptv.dao.VodDao;
import com.unis.iptv.domain.CastRoleMap;
import com.unis.iptv.domain.Vod;
import com.unis.iptv.service.VodService;

/**
 * @date 2013-1-5
 * @author 杨飞宇
 */
@Service
public class VodServiceImpl implements VodService {
	private static Log log = LogFactory.getLog("iptvCourse");
	private static Log errLog = LogFactory.getLog("iptvErrCourse");
	@Autowired
	private VodDao vodDao;
	@Autowired
	private CastRoleMapDao castRoleMapDao;
	
	@Override
	public void execute() {
		try {
			String html = getCustomContext(baseUrl
					+ "/iptvepg/frame1008/vod_portal.jsp");
			String regexNumPerPage = "eval\\(\"\\(\\[\\{'pageCount':'(\\d+)','pageNum':'(\\d+)'";
			// 匹配点播分类总数pageCount * pageNum
			Pattern p = Pattern.compile(regexNumPerPage);
			Matcher m = p.matcher(html);
			int pageCount = 0;
			int pageNum = 0;
			if (m.find()) {
				pageCount = Integer.parseInt(m.group(1));
				pageNum = Integer.parseInt(m.group(2));
			}
			html = getCustomContext(baseUrl
					+ "/iptvepg/frame1008/get_vodColumn_data.jsp?destPage=1&numPerPage="
					+ pageCount * pageNum + "&from=vod_portal");
			String regexPathColumId = "\\{'columnId':'(\\w+)','columnName':'([^']+)'\\}";
			p = Pattern.compile(regexPathColumId);
			m = p.matcher(html);
			while (m.find()) {
				String columnId = m.group(1);
				String path = m.group(2);
				log.info("提取点播子分类：" + path + columnId);
				if (path.equals("热门")) {
					continue;
				}
				log.info("@@@@@@@@@@@@提取" + path + "子分类"); // 娱乐
				String posterHtml = getCustomContext(baseUrl
						+ "/iptvepg/frame1008/vod_poster_list.jsp?columnId="
						+ columnId + "&path=" + URLEncoder.encode(path, "GBK"));
				// 匹配电影等子分类总数pageCount * pageNum
				Pattern pPoster = Pattern.compile(regexNumPerPage);
				Matcher mPoster = pPoster.matcher(posterHtml);
				if (mPoster.find()) {
					pageCount = Integer.parseInt(mPoster.group(1));
					pageNum = Integer.parseInt(mPoster.group(2));
				}
				String secondColumnHtml = getCustomContext(baseUrl
						+ "/iptvepg/frame1008/get_vod_secondColumn.jsp?destPage=1&numPerPage="
						+ pageCount * pageNum + "&columnId=" + columnId);
				pPoster = Pattern.compile(regexPathColumId);
				mPoster = pPoster.matcher(secondColumnHtml);
				while (mPoster.find()) {
					String vodSecondColumnId = mPoster.group(1);
					String vodSecondPath = mPoster.group(2);
					log.info("#####################提取" + path + "子分类"
							+ vodSecondPath + vodSecondColumnId);
					String secondPosterHtml = getCustomContext(baseUrl
							+ "/iptvepg/frame1008/vod_poster_list.jsp?columnId="
							+ columnId + "&path="
							+ URLEncoder.encode(path, "GBK")
							+ "&secondColumnId=" + vodSecondColumnId);
					// 匹配节目总数pageCount * pageNum
					String regexVods = "columnName':'\\S+'}]},\\{'pageCount':'(\\d+)','pageNum':'(\\d+)','data'.*";
					Pattern pVods = Pattern.compile(regexVods);
					Matcher mVods = pVods.matcher(secondPosterHtml);
					int pageCountVods = 0;
					int pageNumVods = 0;
					if (mVods.find()) {
						pageCountVods = Integer.parseInt(mVods.group(1));
						pageNumVods = Integer.parseInt(mVods.group(2));
					}
					
					String vodsHtml = getCustomContext(baseUrl
							+ "/iptvepg/frame1008/get_vod_data.jsp?destPage=1&numPerPage="
							+ pageCountVods * pageNumVods
							+ "&from=vod_portal&columnId=" + vodSecondColumnId);
					String regexVod = "\\{'columnId':'(\\S+)', 'programId':'(\\S+)', 'programName':'(\\S+)', 'programType':'(\\S+)', 'smallPoster':'(\\S+)','normalPoster':'(\\S+)','bigPoster':'(\\S+)','createTime':'(\\S+)','vodType':'(\\S+)'\\}";
					Pattern pVod = Pattern.compile(regexVod);
					Matcher mVod = pVod.matcher(vodsHtml);
					while (mVod.find()) {
						Vod vod = new Vod();
						// String columnIdVod = mVod.group(1); // 现在也没用
						String vodName = mVod.group(3);
						vod.setVodName(vodName);
						vod.setCategoryName(path);
						// String createTime = mVod.group(8);
						// Timestamp licensingWindowStart; 要转下格式
						// vod.setLicensingWindowStart(licensingWindowStart);
						// vod.setVolumnCount(volumnCount); 电视剧的时候再说
						String programType = mVod.group(4);
						// String vodType = mVod.group(9); 现在无用
						if (programType.equals("14")) {
							vod.setSeriesFlag(1);
						} else {
							vod.setVolumnCount(0);
							vod.setSeriesFlag(0);
						}
						
						String code = mVod.group(2);
						vod.setCode(code);
						String smallPoster = mVod.group(5);// 图片 现在都还没用
						System.out.println("smallPoster :" + smallPoster);
						String normalPoster = mVod.group(6);
						System.out.println("normalPoster :" + normalPoster);
						String bigPoster = mVod.group(7);
						System.out.println("bigPoster :" + bigPoster);
						String picPath = downloadPictureByUrl(baseUrl
								+ "/iptvepg/images/poster"
								+ bigPoster.substring(bigPoster
										.lastIndexOf("/")));
						vod.setPicPath(picPath);
						System.out.println("清屏");
						String htmlDetail = getCustomContext(baseUrl
								+ "/iptvepg/frame1008/vod_detail.jsp?columnid="
								+ columnId + "&programid=" + code
								+ "&programtype=" + programType);
						String regexDetail = "<div.*>(\\S+)</div>\\s*<.*>(\\S+)</div>\\s*<.*>(\\S+)</div>\\s*<.*>(\\S+)</div>\\s*<.*>(\\S+)</div>";
						System.out.println("清屏");
						Pattern pDetail = Pattern.compile(regexDetail);
						Matcher mDetail = pDetail.matcher(htmlDetail);
						String director = "";
						String actor = "";
						if (mDetail.find()) {
							vod.setEllipsisName(mDetail.group(1));
							if (programType.equals("1"))
								vod.setDuration(mDetail.group(2));
							actor = mDetail.group(3);
							director = mDetail.group(4);
							vod.setDescription(mDetail.group(5));
						}
						String regexPlay = "var playUrl = \"(\\S+)\"\\s*\\+\"(\\S+)\"";
						pDetail = Pattern.compile(regexPlay);
						mDetail = pDetail.matcher(htmlDetail);
						if (mDetail.find()) {
							String authchoice = mDetail.group(1);
							String params = mDetail.group(2);
							String htmlPlay = getCustomContext(baseUrl
									+ "/iptvepg/frame1008/" + authchoice
									+ params);
							// 电视剧进这里
							if (programType.equals("14")) {
								vod.setSeriesFlag(1);
								String seriesDataRegex = "columnId':'(\\S+)', 'programId':'(\\S+)','programName':'(\\S+)','programType':'14','seriesNum':'(\\d+)','bigPoster':'(\\S+)','contentId':'(\\S+)'\\}";
								pDetail = Pattern.compile(seriesDataRegex);
								mDetail = pDetail.matcher(htmlPlay);
								if (mDetail.find()) {
									String seriesColumnId = mDetail.group(1);
									String seriesProgramId = mDetail.group(2);
									String seriesNum = mDetail.group(4);
									int seriesTotal = Integer
											.parseInt(seriesNum);
									vod.setVolumnCount(seriesTotal);
									// String bigPoster = mDetail.group(5);
									String fatherContentId = mDetail.group(6);
									String seriesDataHtml = getCustomContext(baseUrl
											+ "/iptvepg/frame1008/get_series_data.jsp?vodpage=1&numperpage="
											+ seriesTotal
											+ "&columnid="
											+ seriesColumnId
											+ "&programid="
											+ seriesProgramId);
									seriesDataRegex = "\\{'columnId':'(\\S+)', 'programId':'(\\S+)','seriesNum':'(\\d+)','programType':'(\\S+)','contentId':'(\\S+)','seriesProgramCode':'(\\S+)'\\}";
									pDetail = Pattern.compile(seriesDataRegex);
									mDetail = pDetail.matcher(seriesDataHtml);
									String playLinks = "";
									while (mDetail.find()) {
										String subProgramId = mDetail.group(2);
										String subSeriesNum = mDetail.group(3);
										String subProgramType = mDetail
												.group(4);
										String subContentId = mDetail.group(5);
										// 抓包的 如果播放不了 再取几个参数
										// /iptvepg/frame1008/sitcom_to_mark.jsp?columnid=050C0F0C&programid=0000000030100000012271&vodtype=100&FatherContent=0000000030140000012267&CategoryID=050C0F0C&ContentType=10&ContentID=0000000030100000012271&Seriesnum=2&programtype=10&path=%B5%E3%B2%A5-%B5%E7%CA%D3%BE%E7-%C4%DA%B5%D8
										String seriesPlay = getCustomContext(baseUrl
												+ "/iptvepg/frame1008/sitcom_to_mark.jsp?columnid="
												+ seriesColumnId
												+ "&programid="
												+ subProgramId
												+ "&vodtype=100&FatherContent="
												+ fatherContentId
												+ "&CategoryID="
												+ seriesColumnId
												+ "&ContentID="
												+ subContentId
												+ "&Seriesnum="
												+ subSeriesNum
												+ "&programtype="
												+ subProgramType
												+ "&breakpoint=0");
										String regexPlayLink = "vodPlay\\(\"(\\S+)\"\\)";
										Pattern pSeries = Pattern
												.compile(regexPlayLink);
										Matcher mSeries = pSeries
												.matcher(seriesPlay);
										if (mSeries.find()) {
											String playLink = mSeries.group(1);
											playLinks = playLinks + playLink
													+ "|";
										}
									}
									vod.setPlayLink(playLinks);
									int vodId = vodDao.getVodId();
									vod.setVodId(vodId);
									vodDao.saveVod(vod);
									CastRoleMap castRoleMap1 = new CastRoleMap();
									castRoleMap1.setRole("演员");
									castRoleMap1.setCast(actor);
									castRoleMap1.setVodId(vodId);
									if (null != actor && actor != "") {
										
										castRoleMapDao
												.saveCastRoleMap(castRoleMap1);
									}
									CastRoleMap castRoleMap2 = new CastRoleMap();
									castRoleMap2.setRole("导演");
									castRoleMap2.setCast(director);
									castRoleMap2.setVodId(vodId);
									if (null != director && director != "") {
										
										castRoleMapDao
												.saveCastRoleMap(castRoleMap2);
									}
									
								}
								
							} else {
								// 电影分类等
								String regexPlayLink = "vodPlay\\(\"(\\S+)\"\\)";
								pDetail = Pattern.compile(regexPlayLink);
								mDetail = pDetail.matcher(htmlPlay);
								if (mDetail.find()) {
									String playLink = mDetail.group(1);
									vod.setPlayLink(playLink);
								}
								int vodId = vodDao.getVodId();
								vod.setVodId(vodId);
								vodDao.saveVod(vod);
								CastRoleMap castRoleMap1 = new CastRoleMap();
								castRoleMap1.setRole("演员");
								castRoleMap1.setCast(actor);
								castRoleMap1.setVodId(vodId);
								if (null != actor && actor != "") {
									
									castRoleMapDao
											.saveCastRoleMap(castRoleMap1);
								}
								CastRoleMap castRoleMap2 = new CastRoleMap();
								castRoleMap2.setRole("导演");
								castRoleMap2.setCast(director);
								castRoleMap2.setVodId(vodId);
								if (null != director && director != "") {
									
									castRoleMapDao
											.saveCastRoleMap(castRoleMap2);
								}
							}
							
						}
						
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			errLog.error("点播提取失败：" + e.getMessage());
		}
		
	}
	
	public static void main(String[] args) throws Exception {
		GlobalUtil.initConfiguration();
		Authentication.Auth();
		
		try {
			String html = getCustomContext(baseUrl
					+ "/iptvepg/frame1008/vod_portal.jsp");
			String regexNumPerPage = "eval\\(\"\\(\\[\\{'pageCount':'(\\d+)','pageNum':'(\\d+)'";
			// 匹配点播分类总数pageCount * pageNum
			Pattern p = Pattern.compile(regexNumPerPage);
			Matcher m = p.matcher(html);
			int pageCount = 0;
			int pageNum = 0;
			if (m.find()) {
				pageCount = Integer.parseInt(m.group(1));
				pageNum = Integer.parseInt(m.group(2));
			}
			html = getCustomContext(baseUrl
					+ "/iptvepg/frame1008/get_vodColumn_data.jsp?destPage=1&numPerPage="
					+ pageCount * pageNum + "&from=vod_portal");
			String regexPathColumId = "\\{'columnId':'(\\w+)','columnName':'([^']+)'\\}";
			p = Pattern.compile(regexPathColumId);
			m = p.matcher(html);
			while (m.find()) {
				String columnId = m.group(1);
				String path = m.group(2);
				log.info("提取点播子分类：" + path + columnId);
				if (path.equals("热门")) {
					continue;
				}
				if (path.equals("电影")) {
					continue;
				}
				log.info("@@@@@@@@@@@@提取" + path + "子分类"); // 娱乐
				String posterHtml = getCustomContext(baseUrl
						+ "/iptvepg/frame1008/vod_poster_list.jsp?columnId="
						+ columnId + "&path=" + URLEncoder.encode(path, "GBK"));
				// 匹配电影等子分类总数pageCount * pageNum
				Pattern pPoster = Pattern.compile(regexNumPerPage);
				Matcher mPoster = pPoster.matcher(posterHtml);
				if (mPoster.find()) {
					pageCount = Integer.parseInt(mPoster.group(1));
					pageNum = Integer.parseInt(mPoster.group(2));
				}
				String secondColumnHtml = getCustomContext(baseUrl
						+ "/iptvepg/frame1008/get_vod_secondColumn.jsp?destPage=1&numPerPage="
						+ pageCount * pageNum + "&columnId=" + columnId);
				pPoster = Pattern.compile(regexPathColumId);
				mPoster = pPoster.matcher(secondColumnHtml);
				while (mPoster.find()) {
					String vodSecondColumnId = mPoster.group(1);
					String vodSecondPath = mPoster.group(2);
					log.info("#####################提取" + path + "子分类"
							+ vodSecondPath + vodSecondColumnId);
					String secondPosterHtml = getCustomContext(baseUrl
							+ "/iptvepg/frame1008/vod_poster_list.jsp?columnId="
							+ columnId + "&path="
							+ URLEncoder.encode(path, "GBK")
							+ "&secondColumnId=" + vodSecondColumnId);
					// 匹配节目总数pageCount * pageNum
					String regexVods = "columnName':'\\S+'}]},\\{'pageCount':'(\\d+)','pageNum':'(\\d+)','data'.*";
					Pattern pVods = Pattern.compile(regexVods);
					Matcher mVods = pVods.matcher(secondPosterHtml);
					int pageCountVods = 0;
					int pageNumVods = 0;
					if (mVods.find()) {
						pageCountVods = Integer.parseInt(mVods.group(1));
						pageNumVods = Integer.parseInt(mVods.group(2));
					}
					
					String vodsHtml = getCustomContext(baseUrl
							+ "/iptvepg/frame1008/get_vod_data.jsp?destPage=1&numPerPage="
							+ pageCountVods * pageNumVods
							+ "&from=vod_portal&columnId=" + vodSecondColumnId);
					String regexVod = "\\{'columnId':'(\\S+)', 'programId':'(\\S+)', 'programName':'(\\S+)', 'programType':'(\\S+)', 'smallPoster':'(\\S+)','normalPoster':'(\\S+)','bigPoster':'(\\S+)','createTime':'(\\S+)','vodType':'(\\S+)'\\}";
					Pattern pVod = Pattern.compile(regexVod);
					Matcher mVod = pVod.matcher(vodsHtml);
					while (mVod.find()) {
						Vod vod = new Vod();
						// String columnIdVod = mVod.group(1); 现在也没用
						String vodName = mVod.group(3);
						vod.setVodName(vodName);
						vod.setCategoryName(path);
						// String createTime = mVod.group(8);
						// Timestamp licensingWindowStart; 要转下格式
						// vod.setLicensingWindowStart(licensingWindowStart);
						// vod.setVolumnCount(volumnCount); 电视剧的时候再说
						String programType = mVod.group(4);
						// String vodType = mVod.group(9); 现在无用
						if (programType.equals("14")) {
							vod.setSeriesFlag(1);
						} else {
							vod.setVolumnCount(0);
							vod.setSeriesFlag(0);
						}
						
						String code = mVod.group(2);
						vod.setCode(code);
						
						// String smallPoster = mVod.group(5);图片 现在都还没用
						// String normalPoster = mVod.group(6);
						// String bigPoster = mVod.group(7);
						// String picPath;
						// vod.setPicPath(picPath);
						System.out.println("清屏");
						String htmlDetail = getCustomContext(baseUrl
								+ "/iptvepg/frame1008/vod_detail.jsp?columnid="
								+ columnId + "&programid=" + code
								+ "&programtype=" + programType);
						String regexDetail = "<div.*>(\\S+)</div>\\s*<.*>(\\S+)</div>\\s*<.*>(\\S+)</div>\\s*<.*>(\\S+)</div>\\s*<.*>(\\S+)</div>";
						System.out.println("清屏");
						Pattern pDetail = Pattern.compile(regexDetail);
						Matcher mDetail = pDetail.matcher(htmlDetail);
						String director = "";
						String actor = "";
						if (mDetail.find()) {
							vod.setEllipsisName(mDetail.group(1));
							if (programType.equals("1"))
								vod.setDuration(mDetail.group(2));
							actor = mDetail.group(3);
							director = mDetail.group(4);
							vod.setDescription(mDetail.group(5));
						}
						String regexPlay = "var playUrl = \"(\\S+)\"\\s*\\+\"(\\S+)\"";
						pDetail = Pattern.compile(regexPlay);
						mDetail = pDetail.matcher(htmlDetail);
						if (mDetail.find()) {
							String authchoice = mDetail.group(1);
							String params = mDetail.group(2);
							String htmlPlay = getCustomContext(baseUrl
									+ "/iptvepg/frame1008/" + authchoice
									+ params);
							// 电视剧进这里
							if (programType.equals("14")) {
								vod.setSeriesFlag(1);
								String seriesDataRegex = "columnId':'(\\S+)', 'programId':'(\\S+)','programName':'(\\S+)','programType':'14','seriesNum':'(\\d+)','bigPoster':'(\\S+)','contentId':'(\\S+)'\\}";
								pDetail = Pattern.compile(seriesDataRegex);
								mDetail = pDetail.matcher(htmlPlay);
								if (mDetail.find()) {
									String seriesColumnId = mDetail.group(1);
									String seriesProgramId = mDetail.group(2);
									String seriesNum = mDetail.group(4);
									int seriesTotal = Integer
											.parseInt(seriesNum);
									vod.setVolumnCount(seriesTotal);
									// String bigPoster = mDetail.group(5);
									String fatherContentId = mDetail.group(6);
									String seriesDataHtml = getCustomContext(baseUrl
											+ "/iptvepg/frame1008/get_series_data.jsp?vodpage=1&numperpage="
											+ seriesTotal
											+ "&columnid="
											+ seriesColumnId
											+ "&programid="
											+ seriesProgramId);
									seriesDataRegex = "\\{'columnId':'(\\S+)', 'programId':'(\\S+)','seriesNum':'(\\d+)','programType':'(\\S+)','contentId':'(\\S+)','seriesProgramCode':'(\\S+)'\\}";
									pDetail = Pattern.compile(seriesDataRegex);
									mDetail = pDetail.matcher(seriesDataHtml);
									String playLinks = "";
									while (mDetail.find()) {
										String subProgramId = mDetail.group(2);
										String subSeriesNum = mDetail.group(3);
										String subProgramType = mDetail
												.group(4);
										String subContentId = mDetail.group(5);
										// 抓包的 如果播放不了 再取几个参数
										// /iptvepg/frame1008/sitcom_to_mark.jsp?columnid=050C0F0C&programid=0000000030100000012271&vodtype=100&FatherContent=0000000030140000012267&CategoryID=050C0F0C&ContentType=10&ContentID=0000000030100000012271&Seriesnum=2&programtype=10&path=%B5%E3%B2%A5-%B5%E7%CA%D3%BE%E7-%C4%DA%B5%D8
										String seriesPlay = getCustomContext(baseUrl
												+ "/iptvepg/frame1008/sitcom_to_mark.jsp?columnid="
												+ seriesColumnId
												+ "&programid="
												+ subProgramId
												+ "&vodtype=100&FatherContent="
												+ fatherContentId
												+ "&CategoryID="
												+ seriesColumnId
												+ "&ContentID="
												+ subContentId
												+ "&Seriesnum="
												+ subSeriesNum
												+ "&programtype="
												+ subProgramType
												+ "&breakpoint=0");
										String regexPlayLink = "vodPlay\\(\"(\\S+)\"\\)";
										Pattern pSeries = Pattern
												.compile(regexPlayLink);
										Matcher mSeries = pSeries
												.matcher(seriesPlay);
										if (mSeries.find()) {
											String playLink = mSeries.group(1);
											playLinks = playLinks + playLink
													+ "|";
										}
									}
									vod.setPlayLink(playLinks);
									// int vodId = vodDao.getVodId();
									// vod.setVodId(vodId);
									// vodDao.saveVod(vod);
									CastRoleMap castRoleMap1 = new CastRoleMap();
									castRoleMap1.setRole("演员");
									castRoleMap1.setCast(actor);
									// castRoleMap1.setVodId(vodId);
									// castRoleMapDao.saveCastRoleMap(castRoleMap1);
									CastRoleMap castRoleMap2 = new CastRoleMap();
									castRoleMap2.setRole("导演");
									castRoleMap2.setCast(director);
									// castRoleMap2.setVodId(vodId);
									// castRoleMapDao.saveCastRoleMap(castRoleMap2);
									
								}
								
							} else {
								// 电影分类等
								String regexPlayLink = "vodPlay\\(\"(\\S+)\"\\)";
								pDetail = Pattern.compile(regexPlayLink);
								mDetail = pDetail.matcher(htmlPlay);
								if (mDetail.find()) {
									String playLink = mDetail.group(1);
									vod.setPlayLink(playLink);
								}
								// int vodId = vodDao.getVodId();
								// vod.setVodId(vodId);
								// vodDao.saveVod(vod);
								CastRoleMap castRoleMap1 = new CastRoleMap();
								castRoleMap1.setRole("演员");
								castRoleMap1.setCast(actor);
								// castRoleMap1.setVodId(vodId);
								// castRoleMapDao.saveCastRoleMap(castRoleMap1);
								CastRoleMap castRoleMap2 = new CastRoleMap();
								castRoleMap2.setRole("导演");
								castRoleMap2.setCast(director);
								// castRoleMap2.setVodId(vodId);
								// castRoleMapDao.saveCastRoleMap(castRoleMap2);
							}
							
						}
						
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			errLog.error("点播提取失败：" + e.getMessage());
		}
		
	}
}
